<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import the Joomla modellist library
jimport('joomla.application.component.modellist');
/**
 * Publications list Model
 */
class NrmResearchModelCurrentResearch extends JModelList
{
	/**
	 * Method to build an SQL query to load the list data.
	 *
	 * @return	string	An SQL query
	 */
	protected function getListQuery()
	{
		// Create a new query object.		
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		// Select some fields
		$query->select('a.*');		
		
		$query->from('#__nrmresearch_'.$this->getState('category','').' as a');
				
		// Filter by contact_name
		if ($this->getState('keywords')) {
			$query->where('a.keywords like "%'.$this->getState('keywords').'%"');
		}
		
		//Add the list ordering clause.
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.id')).' '.$db->getEscaped($this->getState('list.direction', 'ASC')));
		//echo "start = ". $this->getState("list.start") . "    and limit = " . $this->getState("list.limit") ;
		return $query;
	}
//	public function &getItems()
//	{
// 		// Invoke the parent getItems method to get the main list
// 		$items = &parent::getItems();
	
// 		// Convert the params field into an object, saving original in _params
// 		for ($i = 0, $n = count($items); $i < $n; $i++) {
// 			$item = &$items[$i];
// 			//			if (!isset($this->_params)) {
// 			//				$params = new JRegistry();
// 			//				$params->loadString($item->params);
// 			//				$item->params = $params;
// 			//			}
// 		}
	
// 		return $items;
//	}
	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @since	1.6
	 */
	protected function populateState($ordering = null, $direction = null)
	{
		// Initialise variables.
		$app	= JFactory::getApplication();
		//$params	= JComponentHelper::getParams('com_meeting');
		
		
		// Load the parameters. 
		$params = $app->getParams();		
		$this->setState('params', $params);		
		
		$db		= $this->getDbo();
		$limit = $app->getUserStateFromRequest('global.list.limit', 'limit', 5);
		$this->setState('list.limit', $limit);
		
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
		$this->setState('list.start', $limitstart);
		
		$orderCol	= JRequest::getCmd('filter_order', 'id');
		if (!in_array($orderCol, $this->filter_fields)) {
			$orderCol = 'id';
		}
		$this->setState('list.ordering', $orderCol);
		
		$category = JRequest::getVar('filter_category','currentproject');
		$this->setState('category',$category);
	
		$listOrder	=  JRequest::getCmd('filter_order_Dir', 'DESC');
		if (!in_array(strtoupper($listOrder), array('ASC', 'DESC', ''))) {
			$listOrder = 'ASC';
		}
		$this->setState('list.direction', $listOrder);
	
		$publication_keywords = JRequest::getVar('keywords','','','string');
		$this->setState('keywords',$publication_keywords);
		
		// Load the parameters.
		//$this->setState('params', $params);
	}	
	function getPagination()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_pagination)) {
			require_once(JPATH_COMPONENT.DS.'libs'.DS.'pagination.php');
			$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
			$this->_pagination = new JPagination4Grid($this->getTotal(), $limitstart, 5 );
		}
		return $this->_pagination;
	}
}
